package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.domain.EthMarketPriceDomain;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class MarketPriceListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<EthMarketPriceDomain.DataBean> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public Context context;

   public MarketPriceListAdapter(Context context) {
       inflater = LayoutInflater.from(context);
      this.context=context;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_market_price, parent, false));
       return vh;
   }
   @SuppressLint("SetTextI18n")
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
                   IndexHolder ih= (IndexHolder) viewHolder;
       EthMarketPriceDomain.DataBean    dataBean= dataBeanList.get(i);
       DecimalFormat df=new DecimalFormat("#.####");
       DecimalFormat df1=new DecimalFormat("#.##");
                 ih.name.setText(dataBean.getName());
                 ih.symbol.setText(dataBean.getSymbol());
                 ih.cny.setText("¥ "+df.format(dataBean.getQuotes().getCNY().getPrice()));
                 ih.usd.setText("$ "+df1.format(dataBean.getQuotes().getUSD().getPrice()));

                 if(dataBean.getQuotes().getCNY().getPercent_change_24h()>0){
                    ih.percent.setBackgroundResource(R.drawable.shape_round_default_green_5dp);
                    ih.percent.setText("+"+dataBean.getQuotes().getCNY().getPercent_change_24h()+"%");
                 }else{
                     ih.percent.setBackgroundResource(R.drawable.shape_round_default_red_1dp);
                     ih.percent.setText("-"+dataBean.getQuotes().getCNY().getPercent_change_24h()+"%");
                 }
   }

   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<EthMarketPriceDomain.DataBean> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void appendList(List<EthMarketPriceDomain.DataBean> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }


   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
             public TextView name,symbol;
             public  TextView cny,usd;
             public  TextView percent;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
            name=itemView.findViewById(R.id.name);
            symbol=itemView.findViewById(R.id.symbol);
             cny=itemView.findViewById(R.id.cny);
            usd=itemView.findViewById(R.id.usd);
            percent=itemView.findViewById(R.id.percent);
        }
   }

}